package com.his.back.service.serviceImpl;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.his.back.dao.*;
import com.his.back.pojo.*;
import com.his.back.service.MenZhenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MenZhenServiceimpl implements MenZhenService{
    @Autowired
    memberMapper memberMapper;
    @Autowired
    projectMapper projectMapper;
    @Autowired
    patient_projectMapper patient_projectMapper;
    @Autowired
    tempateMapper tempateMapper;
    @Autowired
    patient_drugMapper patient_drugMapper;
    @Autowired
    drugMapper drugMapper;

    @Override
    public List<member> searchPatient(String content){
        return memberMapper.searchPatient(content);
    }

    @Override
    public List<member> getUserMemberList(String userId, int stateId){
        return memberMapper.getUserMemberList(userId,stateId);
    }

    @Override
    public List<member> getDeptMemberList(String deptId, int stateId){
        return memberMapper.getDeptMemberList(deptId,stateId);
    }

    @Override
    public int updatePatientInfo(String patientId, String patientInfo){
        return memberMapper.updatePatientInfo(patientId,patientInfo);
    }

    @Override
    public int updatePatientSolve(String patientId, String solveXi, String solveZhong){
        return memberMapper.updatePatientSolve(patientId, solveXi, solveZhong);
    }

    @Override
    public List<project> selectProByType(String projType) {
        return projectMapper.selectProByType(projType);
    }

    @Override
    public String selectProResult(String patientId, String projName) {
        return patient_projectMapper.selectProResult(patientId,projName);
    }

    @Override
    public int insertPro(String patientId, String projName){
        Date time = new Date();
        return patient_projectMapper.insertPro(patientId,projName,time);
    }

    @Override
    public int deletePro(String patientId, String projName){
        patient_projectKey patient_projectKey = new patient_projectKey();
        patient_projectKey.setPatientId(patientId);
        patient_projectKey.setProjectName(projName);
        return patient_projectMapper.deleteByPrimaryKey(patient_projectKey);
    }

    @Override
    public List<tempate> selectTemByType(String type){
        return tempateMapper.selectTemByType(type);
    }

    @Override
    public List<project> getTemProList(String temName, String type){
        String proNamaeList = tempateMapper.selectProList(temName);
        System.out.println(proNamaeList);
        String []proNameList = proNamaeList.split(",");
        ArrayList<project> proList = new ArrayList<>();
        for(int i = 0; i < proNameList.length; i ++){
            proList.add(projectMapper.selectByPrimaryKey(proNameList[i]));
        }
        return proList;
    }

    @Override
    public List<procost> selectProCost(String patientId, String type){
        if(type.equals("任何")){
            return patient_projectMapper.selectAllProCost(patientId);
        }else{
            return patient_projectMapper.selectProCostByType(patientId,type);
        }
    }

    @Override
    public String selectSolveXi(String patientId){
        return memberMapper.selectSolveXi(patientId);
    }

    @Override
    public String selectSolveZhong(String patientId){
        return memberMapper.selectSolveZhong(patientId);
    }

    @Override
    public List<Map<String,String>> selectJianchaData(String patientId){
        return patient_projectMapper.selectJianchaData(patientId);
    }

    @Override
    public List<Map<String,String>> selectJianyanData(String patientId){
        return patient_projectMapper.selectJianyanData(patientId);
    }

    @Override
    public List<drug> selectDrugByType(String type){
        if(type.equals("zhong")){
            return drugMapper.selectDrugByType("中");
        }else{
            return drugMapper.selectDrugByType("西");
        }
    }

    @Override
    public List<Map<String,String>> selectTemByName(String temName,String type){
        return tempateMapper.selectTemByName(temName,type);
    }

    @Override
    public List<drug> searchTemContent(String temName){
        String drugListStr = tempateMapper.selectProList(temName);
        String[] drugNameList = drugListStr.split(",");
        List<drug> drugList = new ArrayList<drug>();
        for(int i  = 0; i < drugNameList.length; i ++){
            drugList.add(drugMapper.selectDrugByName(drugNameList[i]));
        }
        return drugList;
    }

    @Override
    public int updateChufang(String postInfo){
        //System.out.println(postInfo);
        int count = 0;
        JsonParser jp = new JsonParser();
        JsonObject jo = jp.parse(postInfo).getAsJsonObject();
        String patientId = jo.get("patientId").getAsString();
        JsonArray chufangList = jo.get("chufang").getAsJsonArray();
        for(int i = 0; i < chufangList.size(); i ++){
            JsonObject chufang = chufangList.get(i).getAsJsonObject();
            String prescription = chufang.get("name").getAsString();
            JsonArray drugList = chufang.get("drugList").getAsJsonArray();
            for(int j = 0; j < drugList.size(); j ++) {
                JsonObject drug = drugList.get(j).getAsJsonObject();
                //System.out.println(drug);
                String drugName = drug.get("name").getAsString();
                int drugId = drugMapper.selectDrugIdByName(drugName);
                patient_drug pd = new patient_drug();
                pd.setPatientId(patientId);
                pd.setDrugId(drugId);
                pd.setDrugNum(drug.get("num").getAsInt());
                pd.setPrescription(prescription);
                pd.setDrugDatetime(new Date());
                count += patient_drugMapper.insert(pd);
            }
        }
        return count;
    }

    @Override
    public int deleteChufang(String postInfo){
        int count = 0;
        JsonParser jp = new JsonParser();
        JsonObject jo = jp.parse(postInfo).getAsJsonObject();
        String patientId = jo.get("patientId").getAsString();
        JsonArray chufangList = jo.get("chufang").getAsJsonArray();
        for(int i = 0; i < chufangList.size(); i ++){
            JsonObject chufang = chufangList.get(i).getAsJsonObject();
            String prescription = chufang.get("name").getAsString();
            JsonArray drugList = chufang.get("drugList").getAsJsonArray();
            for(int j = 0; j < drugList.size(); j ++) {
                JsonObject drug = drugList.get(j).getAsJsonObject();
                String drugName = drug.get("name").getAsString();
                int drugId = drugMapper.selectDrugIdByName(drugName);
                patient_drugKey pdk = new patient_drugKey();
                pdk.setDrugId(drugId);
                pdk.setPatientId(patientId);
                count += patient_drugMapper.deleteByPrimaryKey(pdk);
            }
        }
        return count;
    }

    @Override
    public int updateMemberState(String patientId){
        return memberMapper.updateMemberState(patientId);
    }
}
